package top.yunmouren.areamusic.event;

import io.papermc.paper.event.player.AsyncChatEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import top.yunmouren.areamusic.AreaMusic;
import top.yunmouren.areamusic.commannd.PlayerSelectionCache;
import top.yunmouren.areamusic.config.Priority;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PlayerChatListener implements Listener {
    private final AreaMusic plugin;

    public PlayerChatListener(AreaMusic plugin) {
        this.plugin = plugin;
    }

    @EventHandler
    public void onEvent(AsyncChatEvent event) {
        PlayerSelectionCache.CreateAreaDto createAreaDto = this.plugin.getPlayerSelectionCache().getPlayerHistory(event.getPlayer().getUniqueId().toString());
        if (createAreaDto != null) {
            event.setCancelled(true);
            if (event.message().toString().contains("cancel")) {
                event.setCancelled(true);
                this.plugin.getPlayerSelectionCache().setPlayerHistory(event.getPlayer().getUniqueId().toString(), (PlayerSelectionCache.CreateAreaDto)null);
                event.getPlayer().sendMessage("§a退出创建音乐播放区域交互模式...");
            } else {
                String[] args = event.message().toString().split(" ");
                if (args.length < 2) {
                    event.setCancelled(true);
                    event.getPlayer().sendMessage("§c参数格式错误，正确的格式应该是 键 值（可以是多个值）");
                } else {
                    if (args[0].startsWith("areaId")) {
                        createAreaDto.setAreaId(args[1]);
                        event.getPlayer().sendMessage("成功设置区域 ID");
                    } else if (args[0].startsWith("musicId")) {
                        String[] musicIds = (String[]) Arrays.copyOfRange(args, 1, args.length);
                        List<String> musicUUIDs = new ArrayList();
                        int var7 = musicIds.length;

                        for (String musicId : musicIds) {
                            this.plugin.getConfigManager().getMusics().forEach((key, value) -> {
                                if (value.getMusicId().equals(musicId)) {
                                    musicUUIDs.add(key);
                                }

                            });
                        }

                        createAreaDto.setMusicId(musicUUIDs);
                        event.getPlayer().sendMessage("§a成功设置音乐列表");
                    } else if (args[0].equals("priority")) {
                        try {
                            Priority priority = Priority.valueOf(args[1]);
                            createAreaDto.setPriority(priority);
                            event.getPlayer().sendMessage("§a成功设置优先级");
                        } catch (IllegalArgumentException var15) {
                            event.getPlayer().sendMessage("§c未找到目标优先级，设置失败");
                        }
                    } else {
                        boolean overWrite;
                        switch (args[0]) {
                            case "random":
                                try {
                                    overWrite = Boolean.parseBoolean(args[1]);
                                    createAreaDto.setRandom(overWrite);
                                    event.getPlayer().sendMessage("§a成功设置是否进行随机播放");
                                } catch (IllegalArgumentException var14) {
                                    event.getPlayer().sendMessage("§c参数错误");
                                }
                                break;
                            case "loop":
                                try {
                                    overWrite = Boolean.parseBoolean(args[1]);
                                    createAreaDto.setLoop(overWrite);
                                    event.getPlayer().sendMessage("§a成功设置是否进行循环播放");
                                } catch (IllegalArgumentException var13) {
                                    event.getPlayer().sendMessage("§c参数错误");
                                }
                                break;
                            case "enabled":
                                try {
                                    overWrite = Boolean.parseBoolean(args[1]);
                                    createAreaDto.setEnabled(overWrite);
                                    event.getPlayer().sendMessage("§a成功设置是否激活该区域");
                                } catch (IllegalArgumentException var12) {
                                    event.getPlayer().sendMessage("§c参数错误");
                                }
                                break;
                            case "overWrite":
                                try {
                                    overWrite = Boolean.parseBoolean(args[1]);
                                    createAreaDto.setOverWrite(overWrite);
                                    event.getPlayer().sendMessage("§a成功设置是否能够对正在播放音乐进行覆盖");
                                } catch (IllegalArgumentException var11) {
                                    event.getPlayer().sendMessage("§c参数错误");
                                }
                                break;
                            case "volume":
                                try {
                                    Float volume = Float.parseFloat(args[1]);
                                    createAreaDto.setVolume(volume);
                                    event.getPlayer().sendMessage("§a成功设置音量");
                                } catch (IllegalArgumentException var10) {
                                    event.getPlayer().sendMessage("§c参数错误");
                                }
                                break;
                            case "pitch":
                                try {
                                    float pitch = Float.parseFloat(args[1]);
                                    if (pitch <= 0.0F || pitch > 2.0F) {
                                        event.getPlayer().sendMessage("§c参数错误");
                                        return;
                                    }

                                    createAreaDto.setPitch(pitch);
                                    event.getPlayer().sendMessage("§a成功设置播放速率");
                                } catch (IllegalArgumentException var16) {
                                    event.getPlayer().sendMessage("§c参数错误");
                                }
                                break;
                        }
                    }

                    event.getPlayer().sendMessage(createAreaDto.toString());
                }
            }
        }
    }
}
